package phoenix.web.test_objects;

import java.util.HashMap;

import org.testng.TestException;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.core.helpers.extensions.WebDriverHelperExt;
import phoenix.web.test_objects.data_providers.UserPageDataProviders;

/**
 * Class contains test methods for Index page
 */
public class UserPageTests extends TestBaseWebExt
{
	public UserPageTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	// Data
	public UserPageDataProviders dataProviders = new UserPageDataProviders(manager);

	// TestObjects
	private final MessengerTests messengerTests = new MessengerTests(manager);

	/**
	 * Check chat message communication between paid users
	 * @param p_userSender User page data sender
	 * @param p_userRecipient User page data recipient
	 */
	public void interactionsTest(TestUserDataModel p_userSender, TestUserDataModel p_userRecipient)
	{
		HashMap<String, String> driverHandles = null;
		try
		{
			String p_messageText = "Hello, what do you: " + randomizer().getRandomString(10) +
				randomizer().getRandomInt(1, 1000);
			// Init second window
			WebDriverHelperExt.driverHandles = wd().initSecondBrowser();

			// Prepare for receiving
			wd().getSecondWindow();
			wd().openAutologin(p_userRecipient);
			wd().openUserProfile(p_userSender);
			page().messenger().clickMessenger();

			// Do actions
			wd().getPrimaryWindow();
			// Does autologin
			messengerTests.sendMessage(testUser(), p_userRecipient, p_messageText);

			if (!page().messenger().isMessagePresent(p_messageText))
			{
				String errorText = "Cannot send message to " + p_userRecipient.getId() + " message is not found in messenger";
				log().errorAndScreen(errorText);
				throw new TestException(errorText);
			}
			page().messenger().clickClose();
			// Delay is needed for data sync
			wd().wait(30);

			// Test - is message received
			wd().getSecondWindow();
			// Receiving
			if (manager.web().messenger().isMessagePresent(p_messageText))
			{
				manager.web().messenger().closeMessenger();
			}
			else
			{
				throw new TestException("Cannot find incoming message " + p_messageText + ", from user " + p_userSender.getId().toString());
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("interactionsTest | from id: " + p_userSender.getId() + " > to id: " + p_userRecipient.getId() + " Error: " + p_ex.toString());
		}
		finally
		{
			wd().getSecondWindow();
			wd().close();
			wd().getPrimaryWindow();
			test().sessionRestore(p_userSender);
			test().forErrors();
		}
	}

	public void interactionsPrivateChatTest(TestUserDataModel p_userSender, TestUserDataModel p_userRecipient)
	{
		try
		{
			// Preparation steps
			wd().openAutologin(testUser());
			manager.wd().openPage("/account");
			manager.web().myAccountPage().disableAllNotifications(testUser());
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("interactionsTest_Private_Chat: disable notifications " + p_ex.toString());
		}

		try
		{
			WebDriverHelperExt.driverHandles = wd().initSecondBrowser();
			String p_messageTextChat = "Hello, what do you: " + randomizer().getRandomString(10) + randomizer().getRandomInt(1, 1000);
			String p_messageTextChatAnswer = "Hello, what do you: " + randomizer().getRandomString(10) + randomizer().getRandomInt(1, 1000);
			// Prepare for recipient
			wd().getSecondWindow();
			this.prepeareMeasures(p_userSender, p_userRecipient);
			wd().openPage("/b2b/communication/chat/id/" + p_userRecipient.getId() + "/" + p_userSender.getId());
			// Do actions

			wd().getPrimaryWindow();
			messengerTests.sendChat(p_userSender, p_userRecipient, p_messageTextChat);
			if (!page().messenger().isMessagePresent(p_messageTextChat))
			{
				String errorText =
					"Outgoing message: from " + p_userSender.getId() + " / to " + p_userRecipient.getId() + ", message is not found in messenger";
				log().errorAndScreen(errorText);
				throw new TestException(errorText);
			}

			wd().wait(20);
			wd().getSecondWindow();
			if (!page().messenger().isMessagePresent(p_messageTextChat))
			{
				String errorText =
					"Incoming message: from " + p_userSender.getId() + " / to " + p_userRecipient.getId() + ", message is not found in messenger";
				log().errorAndScreen(errorText);
				throw new TestException(errorText);
			}

			// Answer
			messengerTests.sendChat(p_userRecipient, p_userSender, p_messageTextChatAnswer);
			if (!page().messenger().isMessagePresent(p_messageTextChatAnswer))
			{
				String errorText =
					"Outgoing message: from " + p_userRecipient.getId() + " / to " + p_userSender.getId() + ", message is not found in messenger";
				log().errorAndScreen(errorText);
				throw new TestException(errorText);
			}

			wd().wait(60);

			wd().getPrimaryWindow();
			if (!page().messenger().isMessagePresent(p_messageTextChatAnswer))
			{
				String errorText =
					"Incoming message: from " + p_userRecipient.getId() + " / to " + p_userSender.getId() + ", message is not found in messenger";
				log().errorAndScreen(errorText);
				throw new TestException(errorText);
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("interactionsTest_Private_Chat " + p_ex.toString());
		}
		finally
		{
			wd().getSecondWindow();
			wd().close();
			wd().getPrimaryWindow();
			test().sessionRestore(p_userSender);
			test().forErrors();
		}
	}

	public void interactionLetterTest(TestUserDataModel p_userSender, TestUserDataModel p_userRecipient)
	{
		try
		{
			// Preparation steps
			wd().openAutologin(testUser());
			manager.wd().openPage("/account");
			manager.web().myAccountPage().disableAllNotifications(testUser());
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("interactionsTest_Private_Chat: disable notifications " + p_ex.toString());
		}

		try
		{
			WebDriverHelperExt.driverHandles = wd().initSecondBrowser();

			String p_messageTextLetter = "Hello, what do you: "
				+ manager.randomizer().getRandomString(200)
				+ manager.randomizer().getRandomInt(1, 10);

			String p_messageSubjectLetter = "Hello, : " + randomizer().getRandomString(5) +
				randomizer().getRandomInt(1, 1000);

			wd().getSecondWindow();
			this.prepeareMeasures(p_userSender, p_userRecipient);
			wd().openPage("/b2b/letter/conversation/id/" + p_userRecipient.getId() + "/" + p_userSender.getId());

			// Send letter
			wd().getPrimaryWindow();
			messengerTests.sendLetter(p_userSender, p_userRecipient, p_messageTextLetter, p_messageSubjectLetter);
			if (!page().inboxPage().isLetterPresent(p_messageTextLetter, p_messageSubjectLetter))
			{
				throw new TestException("Outgoing: Unable to find letter or subject");
			}

			wd().wait(20); // Data sync timeout
			wd().getSecondWindow();
			wd().refreshPage();

			if (!page().inboxPage().isLetterPresent(p_messageTextLetter, p_messageSubjectLetter))
			{
				throw new TestException("Incoming: Unable to find letter or subject");
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("interactionsTest_Letter " + p_ex.toString());
		}
		finally
		{
			wd().getSecondWindow();
			wd().close();
			wd().getPrimaryWindow();
			test().sessionRestore(p_userSender);
			test().forErrors();
		}
	}

	/**
	 * Open a page of recipient user and make Sender as paid if Sender is free
	 * @param p_userSender
	 * @param p_userRecipient
	 */
	private void prepeareMeasures(TestUserDataModel p_userSender, TestUserDataModel p_userRecipient)
	{
		log().info("Prepare step before receiving message");

		if (manager.isFGF())
		{
			wd().openPage("/b2b/login");
			manager.web().indexPage().typeLoginEmail("autotesting_manager@co.com");
			manager.web().indexPage().typeLoginPassword("111111");
			manager.web().indexPage().submitLoginFormB2B();
			return;
		}

		wd().openAutologin(p_userRecipient);
		wd().openUserProfile(p_userSender);
		page().messenger().clickMessenger();
	}

	public void testForActivityMessages(TestUserDataModel p_userSender, TestUserDataModel p_userRecipient)
	{
		TestUserDataModel p_originUserModel = testUser();
		try
		{
			try
			{
				// Go to profile of recipient (Browse)
				wd().openURL(p_userRecipient.getAutologin());
				wd().waitForPresence(page().searchPage().elements().blockUserList, null);

				if (!wd().isElementDisplayed(page().indexPage().elements().btnRegSubmit))
				{
					wd().wait(3);
					wd().openPage("/site/logout");
					wd().wait(3);
					wd().openPage("/");
				}
				wd().waitForPresence(page().indexPage().elements().btnRegSubmit, "Submit button");
				wd().wait(60); // time is required for proper sync

				if (log().isErrors())
				{
					throw new TestException("Unable to do logout");
				}
			}
			catch (Exception p_ex)
			{
				throw new TestException(p_ex.toString());
			}

			try
			{
				wd().openURL(p_userSender.getAutologin());
				wd().waitForPresence(page().searchPage().elements().blockUserList, null);
				// Go to profile of recipient (Browse)
				wd().openUserProfile(p_userRecipient);
				wd().waitForVisibility(page().userPage().elements().elementAwayIcon, "Away icon");
				wd().refreshPage();
				// Send a Wink (Wink)
				page().userPage().clickWink();
				wd().wait(3);
				// Test - is message sent (Message)
				String messageText = "Hello, " + randomizer().getRandomString(10) + randomizer().getRandomInt(1, 1000);
				messengerTests.sendMessage(p_userSender, p_userRecipient, messageText);

				if (!page().messenger().isMessagePresent(messageText))
				{
					throw new TestException("Cannot send message to " + p_userRecipient.getId() + " message is not found in messenger");
				}
				page().messenger().clickClose();
			}
			catch (Exception p_ex)
			{
				throw new TestException("Unable to make actions " + p_ex.toString());
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("testForActivityMessages " + p_ex.toString());
		}
		finally
		{
			wd().openAutologin(p_originUserModel);
			test().forErrors();
		}

	}

}